package com.archgeek.java.bytebuffer;

import java.nio.ByteBuffer;

/**
 * @author pizhihui
 * @date 2023-03-08 17:51
 */
public class ByteBufferAllocateTest {


    public static void main(String[] args) {

        int capacity = 1_900_000_000;

        /*
        *
耗时: 1580 ms
========================
耗时: 1485 ms
        *
        * */

        // 堆缓冲区
        long start = System.currentTimeMillis();
        ByteBuffer allocate = ByteBuffer.allocate(capacity);
        for (int i = 0; i < capacity; i++) {
            allocate.put((byte) 123);
        }
        System.out.println("耗时: " + (System.currentTimeMillis()-start) + " ms");

        System.out.println("========================");

        // 直接缓冲区,效率比堆缓冲区快
        long start2 = System.currentTimeMillis();
        ByteBuffer allocateDirect = ByteBuffer.allocateDirect(capacity);
        for (int i = 0; i < capacity; i++) {
            allocateDirect.put((byte) 123);
        }
        System.out.println("耗时: " + (System.currentTimeMillis()-start2) + " ms");

    }

}
